package com.ryskoo.catering.cart.service.flower;

import com.ryskoo.catering.cart.dto.flower.FlowerCartDTO;
import com.ryskoo.catering.cart.dto.flower.FlowerCartDetailDTO;
import com.ryskoo.catering.productattrvalue.domain.ProductAttrValuePO;
import com.ryskoo.catering.productattrvalue.service.IProductAttrValueService;
import com.ryskoo.catering.productmain.domain.ProductMainPO;
import com.ryskoo.catering.productmain.service.IProductMainService;
import com.ryskoo.customer.vendorbasic.service.IVendorBasicService;
import com.ryskoo.framework.core.utils.DoubleFormatUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FlowerCartServiceImpl implements IFlowerCartService {

    @Autowired
    private IProductMainService productMainService;
    @Autowired
    private IProductAttrValueService productAttrValueService;
    @Autowired
    private IVendorBasicService vendorBasicService;

    /**
     * 获取购物车信息
     * 根据前台传过来的购物车信息，获取最新的商品等信息进行更新购物车对象
     * @param cart
     */
    @Override
    public void getCart(FlowerCartDTO cart) {
        if(null == cart){
            return;
        }
        List<FlowerCartDetailDTO> detailList = cart.getDetailList();
        if(CollectionUtils.isEmpty(detailList)){
            cart = null;
            return;
        }

        //商品总金额
        double totalPrice = 0;
        //赠送总积分
        double totalIntegral = 0;

        //不存在购物车明细
        List<FlowerCartDetailDTO> notExistList = new ArrayList<>();
        for(FlowerCartDetailDTO detail : detailList){
            ProductMainPO productMain = productMainService.getByUuid(detail.getProductUuid());

            if(null == productMain){
                notExistList.add(detail);
                continue;
            }

            detail.setProductName(detail.getProductName());
            detail.setIntegral(productMain.getIntegral());
            if(StringUtils.isEmpty(detail.getSkuUuid())){
                //不参与优惠
                if(0==productMain.getPromotionType()){
                    detail.setProductPrice(productMain.getPrice());
                }else{
                    detail.setProductPrice(DoubleFormatUtil.multiply(productMain.getPrice(), productMain.getPromotionRate()));
                }
            } else {
                //查询规格商品
                ProductAttrValuePO pav = productAttrValueService.getByUuid(detail.getSkuUuid());
                if(null == pav){
                    notExistList.add(detail);
                    continue;
                }
                //不参与优惠
                if(0==productMain.getPromotionType()){
                    detail.setProductPrice(pav.getPrice());
                }else{
                    double price = DoubleFormatUtil.multiply(pav.getPrice(), productMain.getPromotionRate());
                    detail.setProductPrice(price);
                }
            }
            detail.setTotalPrice(DoubleFormatUtil.multiply(detail.getProductPrice(), detail.getBuyNum()));

            //对选中的商品计算总和
            if(detail.isChecked()){
                totalPrice = DoubleFormatUtil.add(totalPrice, detail.getTotalPrice());

                double integral = DoubleFormatUtil.multiply(detail.getIntegral(), detail.getBuyNum());
                totalIntegral = DoubleFormatUtil.add(totalIntegral, integral);
            }

        }
        if(notExistList.size() > 0){
            detailList.removeAll(notExistList);
            if(CollectionUtils.isEmpty(detailList)){
                cart = null;
                return;
            }
        }

        cart.setTotalPrice(totalPrice);
        cart.setTotalIntegral(totalIntegral);
        cart.setAllPrice(totalPrice);
    }
}
